#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
    struct com
    {
        bool operator()(const pair<string, int>& as1, const pair<string, int>& as2)
        {
            return as1.second > as2.second || (as1.second == as2.second && as1.first < as2.first);
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string, int> as;

        for (auto e : words)
        {
            as[e]++;
        }
        vector<pair<string, int>> aw(as.begin(), as.end());
        sort(aw.begin(), aw.end(), com());
        vector<string> ad;
        for (int i = 0; i < k; i++)
        {
            ad.push_back(aw[i].first);
        }
        return ad;
    }
};
int main()
{
    Solution as;
    vector<string> word = {"wfwe ","wef ","ewfw ","wefwef ","wefwe "};
    as.topKFrequent(word,5);
    for (auto e : word)
    {
        cout << e;
    }
    cout << endl;
	return 0;
}